package com.hrt.common.db;

import com.btcode.db.executer.IDataExecuter;
import com.btcode.orm.cache.IORMCacheExecuter;
import com.btcode.orm.conver.IModelConver;
import com.btcode.orm.conver.IValueConver;
import com.btcode.orm.executer.DefaultORMExecuter;
import com.btcode.orm.model.IField;
import com.btcode.orm.model.IModel;
import com.hrt.common.safe.IUser;
import com.hrt.common.safe.UserSessionManager;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 业务自定义orm执行器，主要完成约定字段的值自动生成
 *
 * @author gislin
 */
public class OpORMExecuter extends DefaultORMExecuter {

    public OpORMExecuter(IDataExecuter dataExecuter, IModelConver modelConver,
        IORMCacheExecuter ormCache, IValueConver valueConver) {
        super(dataExecuter, modelConver, ormCache, valueConver);
    }

    @Override
    public <T extends IModel> void creates(List<T> models) {

        for (T model : models) {
            prepareCreateModel(model);
        }
        super.creates(models);

    }

    @Override
    public <T extends IModel> void create(T model) {
        prepareCreateModel(model);
        super.create(model);
    }

    @Override
    public <T extends IModel> void update(T model) {
        prepareUpdateModel(model);
        super.update(model);
    }

    public <T extends IModel> void prepareCreateModel(T model) {
        List<IField> fields = model.getFields();

        for (IField field : fields) {

            if ("ID".equals(field.getFieldName())) {
                Object value = model.getFieldValue("ID");

                if (value == null) {
                    model.setFieldValue("ID", UUID.randomUUID().toString());
                }
                continue;
            }

            if ("CREATETIME".equals(field.getFieldName())) {
                Object value = model.getFieldValue("CREATETIME");

                if (value == null) {
                    model.setFieldValue("CREATETIME", new Timestamp(new Date().getTime()));
                }
                continue;
            }

            if ("CREATE_BY".equals(field.getFieldName())) {
                if(model.getFieldValue("CREATE_BY")==null||
                        "".equals(model.getFieldValue("CREATE_BY"))) {
                    if (UserSessionManager.getInstance().getUser() == null) {
                        model.setFieldValue("CREATE_BY", "no-one");
                    }
                    else {
                        IUser user = UserSessionManager.getInstance().getUser();
                        model.setFieldValue("CREATE_BY", user.getId());
                    }
                }
            }
        }
    }

    public <T extends IModel> void prepareUpdateModel(T model) {
        List<IField> fields = model.getFields();

        for (IField field : fields) {

            if ("UPDATETIME".equals(field.getFieldName())) {
                Object value = model.getFieldValue("UPDATETIME");

                if (value == null) {
                    model.setFieldValue("UPDATETIME", new Timestamp(new Date().getTime()));
                }
                continue;
            }

            if ("UPDATE_BY".equals(field.getFieldName())) {
                if(UserSessionManager.getInstance().getUser() == null){
                    model.setFieldValue("UPDATE_BY", "no-one");
                }
                else{
                    IUser user = UserSessionManager.getInstance().getUser();
                    model.setFieldValue("UPDATE_BY", user.getId());
                }
            }
        }
    }
}
